package com.cloud.user.controller;

import com.cloud.common.R;
import com.cloud.excption.BizException;
import com.cloud.mall.user.TbMember;
import com.cloud.user.biz.login.AbstractLogin;
import com.cloud.user.biz.login.Login;
import com.cloud.user.biz.dto.LoginDto;
import com.cloud.user.service.ILoginService;
import com.cloud.user.utils.TokenUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Member;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/7/7 11:11
 * @Desc
 * @Remarks
 */
@RequestMapping("/login")
@RestController
public class LoginController {
    @Autowired
    ILoginService loginService;


    @RequestMapping("/login")
    public R login(@RequestBody @Validated LoginDto authLoginDto, BindingResult bindingResult) {
        authLoginDto.validData(bindingResult);
        Login login = AbstractLogin.loginMap.get(authLoginDto.getLoginType());
        if (login == null) {
            throw new BizException("暂不支持该种登录类型");
        }
        return login.doLogin(authLoginDto);
    }

    //    发送手机验证码
    @RequestMapping("/sendPhoneCode")
    public R sendPhoneCode(@RequestBody LoginDto authLoginDto) {
        if (null == authLoginDto || authLoginDto.getPhone() == null) {
            throw new BizException("手机号不能为空");
        }

        return loginService.sendPhoneCode(authLoginDto.getPhone());
    }

    @RequestMapping("/validToken")
    R validToken(@RequestParam("token") String token) {
        if (!StringUtils.hasLength(token)) {
            new R.Builder().buildCustomize(500, "token为null");
        }
        try {
            Claims claims = TokenUtils.parseToken(token);
            Object timestamp = claims.get("timestamp");
            if (timestamp instanceof Long) {
                Long time = (Long) timestamp;
                //超过8小时
                if (((System.currentTimeMillis() - time) / 1000 / 60 / 60) > 7) {
                    return new R.Builder().buildCustomize(500, "token已经过期");
                } else {
                    return new R.Builder().setData(claims.get("id").toString()).buildOk();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new R.Builder().buildCustomize(500, "解析token异常,error:" + e.getMessage());
        } finally {
        }
        return new R.Builder().buildCustomize(500, "未知错误");
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        TbMember member=new TbMember();
        member.setId(66L);
        member.setUsername("mic");
        map.put("id", member.getId());
        map.put("name", member.getUsername());
//        1627891149422
        Long nowTimeStamp = System.currentTimeMillis();
        System.out.println(nowTimeStamp);
        map.put("timestamp", 1627891149422L);
        String s = TokenUtils.generatorToken(map);
        System.out.println(s);
    }

}
